<template>
  <div id="particulars">
    <v-detail :detailData="inputData"
              :inputConfig="inputConfig"
              :rules="rules"
              ref="input"></v-detail>
  </div>
</template>

<script>
import detail from "commons/Detail";
import baseMixin from "@/mixins/BaseMixin";

export default {
  name: "particulars",
  props: {
    inputData: {
      type: Object
    }
  },
  mixins: [baseMixin],
  data () {
    return {
      inputConfig: [
        [
          {
            type: "input",
            label: this.$t('config.cartonCode'),
            props: "cartonizationkey",
            disabled: true
          },
          {
            type: "select",
            label: this.$t('config.boxGroup'),
            props: "cartonizationgroup",
            options: this.getOptions('queryCartonizationDropDown')
          },
          {
            type: "input",
            label: this.$t('config.cartonType'),
            props: "cartontype",
          },
          {
            type: "input",
            label: this.$t('config.useSequence'),
            props: "usesequence",
          },
          {
            type: "checkbox",
            label: this.$t('config.defaultTrayCartonType'),
            props: "defaultpalletcarton",
          },
          {
            type: "checkbox",
            label: this.$t('config.displayPick'),
            props: "displayrfpick",
          },
          {
            type: "checkbox",
            label: this.$t('config.trayExchangeFollow'),
            props: "containerexchangeflag",
          },
        ],
        [
          {
            type: "input",
            label: this.$t('common.desc'),
            props: "cartondescription",
          },
          {
            type: "input",
            label: this.$t('config.maxCount'),
            props: "maxcount",
          },
          {
            type: "input",
            label: this.$t('config.maxWeight'),
            props: "maxweight",
          },
          {
            type: "input",
            label: this.$t('config.volume'),
            props: "cube",
          },
          {
            type: "checkbox",
            label: this.$t('config.defaultOtherCarton'),
            props: "defaultothercarton",
          },
          {
            type: "checkbox",
            label: this.$t('config.displayPack'),
            props: "displayrfpack",
          },
          {
            type: "checkbox",
            label: this.$t('config.displayLoad'),
            props: "displayrfload",
          },
        ],
        [
          {
            type: "select",
            label: this.$t('config.containerType'),
            props: "containertype",
            options: this.getOptions('querycodeLkup/DROPIDTYPE')
          },
          {
            type: "input",
            label: this.$t('config.length'),
            props: "length",
          },
          {
            type: "input",
            label: this.$t('config.width'),
            props: "width",
          },
          {
            type: "input",
            label: this.$t('config.height'),
            props: "height",
          },
          {
            type: "input",
            label: this.$t('config.tareWeight'),
            props: "tareweight",
          },
        ],
        [
          {
            type: "select",
            label: this.$t('config.volumeUom'),
            props: "cubeuom",
            options: this.getOptions('querycodeLkup/DIMS')
          },
          {
            type: "select",
            label: this.$t('config.dwhUom'),
            props: "dwhuom",
            options: this.getOptions('querycodeLkup/DIMS')
          },
          {
            type: "select",
            label: this.$t('config.weightUom'),
            props: "wgtuom",
            options: this.getOptions('querycodeLkup/WEIGHT')
          }
        ]
      ],
      rules: {
        cartonizationkey: [
          { required: true, message: this.$inputPlease("config.cartonCode"), trigger: 'blur' },
        ],
        cartontype: [
          { required: true, message: this.$inputPlease("config.cartonType"), trigger: 'blur' },
        ],
        cartondescription: [
          { required: true, message: this.$inputPlease("common.desc"), trigger: 'blur' },
        ],
        length: [
          { validator: this.verify, trigger: 'blur' },
        ],
        height: [
          { validator: this.verify, trigger: 'blur' },
        ],
        width: [
          { validator: this.verify, trigger: 'blur' },
        ],
        tareweight: [
          { validator: this.verify, trigger: 'blur' },
        ],
        maxcount: [
          { validator: this.maxcount, trigger: 'blur' },
        ],
        maxweight: [
          { validator: this.maxcount, trigger: 'blur' },
        ],
        cube: [
          { validator: this.maxcount, trigger: 'blur' },
        ],
        usesequence: [
          { required: true, validator: this.maxcount, trigger: 'blur' },
        ],
      },
    }
  },
  computed: {},
  methods: {
    verify (rule, value, callback) {
      if (value === '') {
        this.$message.error(this.$t('app.dataIsNotNull'))
        callback(new Error());
      } else {
        var re = /[^0-9.]/;
        let te = value;
        let flag = re.test(te)
        if (!flag) {
          if (value < 0) {
            this.$message.error(this.$t('app.inputNumberPlease'))
            this.inputData[rule.field] = 0
            callback(new Error());
          } else {
            callback();
          }
        } else {
          this.$message.error(this.$t('app.inputNumberPlease'))
          this.inputData[rule.field] = 0
          callback(new Error());
        }
      }
    },
    maxcount (rule, value, callback) {
      if (value === '') {
        callback(new Error());
      } else {
        var re = /^[0-9]*$/;
        let te = value;
        let flag = re.test(te)
        if (flag) {
          if (value < 0) {
            this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
            this.inputData[rule.field] = 0
            callback(new Error());
          } else {
            callback();
          }
        } else {
          this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
          this.inputData[rule.field] = 0
          callback(new Error());
        }
      }
    },
  },
  mounted () {
  },
  watch: {
    async 'inputData.cartonizationgroup' (data) {
      if (this.$store.state.newFlag) {
        try {
          let res = await this.$api.get(`getMaxUsesequenceByCartonizationgroup/${data}`);
          
            this.inputData.usesequence = res.bean;
          
        } catch (e) {
          this.$message.error(e);
        }
      }
    }

  },
  components: {
    "v-detail": detail
  },
}
</script>

<style scoped>
</style>
